# -*- coding: UTF-8 -*-
import os
import sys

from os.path import dirname
import BasePi3DObjectWrapper as bpi3d

root = dirname(dirname(__file__) )
sys.path.append(root)
os.chdir(root)

'''
Created on 03.02.2013

@author: Peter He�, Germany

samples from vvvv how they do it:

Centered/-/-/Phase=.50/Count=11
0.0455, 0.1364, 0.2273, 0.3182, 0.4091, -0.5000, -0.4091, -0.3182, -0.2273, -0.1364, -0.0455

Centered/-/-/Phase=.75/Count=11
0.2955, 0.3864, 0.4773, -0.4318, -0.3409, -0.2500, -0.1591, -0.0682, 0.0227, 0.1136, 0.2045

'''
import pi3d.shape.Plane as piplane
from BasicObjects import BaseObject
from value.Transform import CargoTransform
from pi3d.Shader import Shader
from pi3d.Texture import Texture


class Quad(BaseObject):
    '''
        Creates a output filled with linear values.
        Calling out, does not recompute the output. The output is always recomputed
        when one of the imput values are changed.
    '''
  
    def __init__(self, 
                 Textures_in=None,  # list 
                 Shaders_in=None):  # list):
        
        BaseObject.__init__(self)
        
        if (Shaders_in != None):
            self._shaders = Shaders_in
        else:
            self._shaders = [Shader("shaders/uv_flat")]
        
        if (Textures_in != None):
            self._textures =  Textures_in
        else:            
            self._textures = [Texture("textures/rock1.jpg")]
        self._transformId = -1;
  
    def create3DObject(self):
        return PlaneWrapper(texture=self._textures[0], shader=self._shaders[0] )


    def update(self, 
               Transform=CargoTransform(), 
               Textures_in=None, 
               Shaders_in=None,
               ShaderCustomData=None):
        '''
        '''
        
        if (Textures_in != None): 
            self._textures = Textures_in

        if (Shaders_in != None): 
            self._shaders = Shaders_in
        
        if (self._transformId == Transform.id) :
            for obj in self.drawObjects:
                obj.draw()  # repaint all the stuff during loop...
            return; #nothing has changed
        
        self._transform = Transform        
        maxcount = Transform.maxcount 
        
        if self.lastMaxCount <> maxcount:
            self.nextMaxCount = maxcount
            self.setup()
            
        # now all objects and can be manipulated
        count = 0
        lenOfTextures = len(self._textures)
        for obj in self.drawObjects:
            # print "setup count: ",count, "\r"
            obj.prepareDraw( count, 
                             self._transform, 
                             self._textures[count % lenOfTextures],
                             ShaderCustomData  )
            obj.draw()
            count +=1
        
        # prepare the next computing slice    
        self.lastMaxCount = self.nextMaxCount


    def dumpSpreads(self):
        '''Shows the internal arrays created in update()'''
        print "plane self.maxcount:", self.maxcount 
        print 'self.x:     ', self.scaleY
        print 'self.y:     ', self.translateY
        print 'self.width: ', self.scaleX
        print 'self.height:', self.scaleY

# -----------------------------------------------------------------------------

class PlaneWrapper(bpi3d.BasePi3DObjectWrapper):

    def __init__(self, 
                 texture=None,              # list of Pi3D texture objects
                 shader=None,               # list of Pi3D shader objects
                 shaderCustomData=None      # list of floats (3,6,9..)
                 ):
        
        self._Plane = piplane.Plane(x= 0, y=0, w=1, h=1, z=0)
        
        bpi3d.BasePi3DObjectWrapper.__init__(
             self, 
             self._Plane,
             texture, 
             shader, 
             shaderCustomData)
